<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let tree = {
        val: 1,
        left: {
          val: 2,
          left: {
            val: 4
          },
          right: {
            val: 5
          }
        },
        right: {
          val: 3,
          left: {
            val: 6
          }
        }
      }
      var hasPathSum = function (root, targetSum) {
        function backTracking(root, sum) {
          //找到那条边
          if (!root.left && !root.right && sum == targetSum) {
            return true
          }
          //sum!=target
          if (!root.left && !root.right) {
            return false
          }
          if (root.left) {
            if (backTracking(root.left, sum + root.left.val)) return true
          }
          if (root.right) {
            if (backTracking(root.right, sum + root.right.val)) return true
          }
          //遍历完所有边，如果没有一条边满足条件进入不了无法return true ，会return undefined因此最后添加一个return false
          return false
        }
        console.log(backTracking(root, root.val))
      }
      hasPathSum(tree, 1)
    </script>
  </body>
</html>
